<?php

namespace app\admin\controller\ucenter;
use app\admin\controller\Base;
use app\admin\model\ucenter\UserLevel;
use think\facade\Request;
use think\facade\View;

class User extends Base
{
    protected $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\ucenter\User;
        $this->validate = new \app\admin\validate\ucenter\User();
        $this->redict = url('index');
    }
    public function index(){
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $option = [
                'where' => [
                    ['email|mobile|username','like',"%".$keys."%"]
                ],
                'with' => ['levels'], // 关联的模型名称
                'visible' => [
                    'levels' => ['level_name']  // 关联的模型名称要显示的字段名，默认展示全部
                ]
            ];
            $list = $this->model->loadList($option);
            if ($list['result'] == 'success'){
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list'], 'count' => $list['page']['total']];
            }else{
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => [], 'count' => 0];
            }
            return $result;
        }
        return view();
    }

    public function add(){
        if (Request::isPost()) {
            $this->scene = 'add';
            return parent::create();
        }
        $userLevel = UserLevel::where('status',1)->select();

        $view = [
            'info' => '',
            'title' => lang('add'),
            'userLevel'=>$userLevel,
        ];
        View::assign($view);
        return view();
    }

    public function edit(){
        if (Request::isPost()) {
            $this->scene = 'edit';
            return parent::modify();
        }
        $info = $this->model->find(Request::get('id'));
        $userLevel = UserLevel::where('status',1)->select();
        unset($info['password']);
        $view = [
            'info' => $info,
            'title' => '修改',
            'userLevel'=>$userLevel,
        ];
        View::assign($view);
        return view('add');

    }
}